package com.zx.Servlet;

import com.google.gson.Gson;
import com.zx.Moudel.Queue;
import com.zx.Service.AdminService;
import com.zx.Service.AminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * com.zx.Servlet
 * 26832
 * zx
 * &#064;description: 管理员操作
 * &#064;author: lxm
 * AdminServlet
 * &#064;create: 2024-06-22 16:11
 **/
@WebServlet("/adminData")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //检查预约表中的数据，查询queue表中的当前日期的数据（数据库中的日期格式为yy-mm-dd），
        // 把数据包装在Queue对象中使用json返回给前端
        //1、获取数据
        resp.setCharacterEncoding("utf-8");
        List<Queue> list = new ArrayList<>();
        AdminService adminService = new AminServiceImpl();
        try {
            list = adminService.selectQueue();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //2、包装为json格式
        String json = null;
        Gson gson = new Gson();
        json = gson.toJson(list);
        resp.getWriter().write(json);
    }
}
